package kr.hkit.androidcomplete02.ch12;

import java.util.ArrayList;

import kr.hkit.androidcomplete02.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class ListAddDelMulti extends Activity {
	ArrayList<String> Items;
	ArrayAdapter<String> aAdapter;
	ListView list;
	EditText ed;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch12_listadddemultil);
	
		ed = (EditText) findViewById(R.id.ch12_edit_newItem);
		
		Items = new ArrayList<String>();
		Items.add("First");
		Items.add("Second");
		Items.add("Third");
		Items.add("Fourth");
		Items.add("Fifth");
		
		aAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, Items);
		list = (ListView)findViewById(R.id.ch12_listadddelview);
		list.setAdapter(aAdapter);
		list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				String text = "Select Items : " + Items.get(position);
				Toast.makeText(getApplication(), text, Toast.LENGTH_SHORT).show();
			}
		});
	
	}
	
public void mOnClick(View v){
		
		switch(v.getId()){
		
		case R.id.ch12_btn_add:
			String text = ed.getText().toString();
			Items.add(text);
			ed.setText("");
			aAdapter.notifyDataSetChanged();
			break;
			
		case R.id.ch12_btn_del:
			SparseBooleanArray sb = list.getCheckedItemPositions();
			if(sb.size() != 0){
				for(int i= list.getCount() -1; i>=0; i--){
					if(sb.get(i)){
						Items.remove(i);
					}
				}
				list.clearChoices();
				aAdapter.notifyDataSetChanged();
			}
			
		}
	}
}
